package com.ziki.test.socket.thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.math.BigInteger;
import java.text.SimpleDateFormat;

import com.ziki.test.socket.MySocket;
import com.ziki.test.socket.ServerMain;
import com.ziki.test.socket.data.DeliverData;
import com.ziki.test.socket.protocol.SMPP;
import com.ziki.test.socket.util.Log;

public class DataSendThread extends Thread {
    private MySocket        mySocket;
    private boolean         runFlag = true;
    
    public DataInputStream  dis;
    
    public DataOutputStream dos;
    private SMPP            smpp    = new SMPP();
    SimpleDateFormat        sdf     = new SimpleDateFormat("yyyyMMddHHmmss");
    
    // public DataRecvThread(Socket incoming) {
    // socket = incoming;
    // try {
    // socket.setSoTimeout(30000);
    // dos = new DataOutputStream(new
    // BufferedOutputStream(socket.getOutputStream()));
    // dis = new DataInputStream(new
    // BufferedInputStream(socket.getInputStream()));
    // this.start();
    // }
    // catch (Exception e) {
    // e.printStackTrace();
    // }
    // }
    
    public DataSendThread(MySocket mySocket) {
        this.mySocket = mySocket;
        this.runFlag = true;
        this.start();
    }
    
    public void run() {
        try {
            while (runFlag) {
                if (!ServerMain.blnIsSendDeliver) {
                    break;
                }
                if (!mySocket.loginFlag) {
                    System.out.println("socket not login!!");
                    this.threadSleep(1000);
                    continue;
                }
                System.out.println("socket login!!");
                DeliverData deliverData = new DeliverData();
                deliverData.serviceCode = "OTA";
                deliverData.coding = 0;
                deliverData.spNumber = "123";
                
                for (int i = 0; i < 10; i++) {
                    deliverData.mobile = String.valueOf(BigInteger.valueOf(1381800000100L)
                            .add(BigInteger.valueOf(i)));
                    deliverData.content = "1#35973900";
                    byte[] data = smpp.getDeliver(deliverData);
                    mySocket.sendData(data);
                    if (ListenThread.isPrintLog(smpp.get_DELIVER()))
                        Log.printDataLog("服务器发送", data);
                    this.threadSleep(100);
                }
                runFlag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void stopRun() {
        this.runFlag = false;
        this.interrupt();
    }
    
    public void threadSleep(int miniSec) {
        try {
            Thread.sleep(miniSec);
        } catch (InterruptedException e) {
        }
    }
}
